package com.lostandfound.dao.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.lostandfound.dao.ReplyDao;
import com.lostandfound.entity.Reply;

@Repository("replyDao")
public class ReplyDaoImpl implements ReplyDao {
	
	private Log logger = LogFactory.getLog(this.getClass());
	
	@Autowired
    private SessionFactory sessionFactory;

	@Override
	public Serializable save(Reply obj) {
		logger.info("发表了一个留言");
		return sessionFactory.getCurrentSession().save(obj);
	}

	@Override
	public void update(Reply obj) {}

	@Override
	public void delete(Reply obj) {}

	@Override
	public Reply selectByPrimary(Long id) {
		return (Reply)sessionFactory.getCurrentSession().load(Reply.class, id);
	}

	@Override
	public List<?> listReplyByPage(Map<String, Object> pagemap) {

		Session session = sessionFactory.getCurrentSession();
		String hql = "from Reply r where r.rstatus = '0' and r.topic.tid = ? order by r.r_create_time asc";
		List<?> list = null;
		if(pagemap != null){
			int page = Integer.parseInt((String)pagemap.get("page"));
			int size = Integer.parseInt((String)pagemap.get("size"));
			Integer tid = Integer.parseInt((String)pagemap.get("tid"));
			Query query = session.createQuery(hql)
					.setLong(0, tid.longValue())
					.setFirstResult((page-1)*size)
					.setMaxResults(size);
			list = query.list();
		}
		return list;
	}

	@Override
	public int Count(Map<String, Object> pagemap) {
		
		Session session = sessionFactory.getCurrentSession();
		String hql = "select count(*) from Reply r where r.rstatus = '0' and r.topic.tid = ? ";
		int count = 0;
		if(pagemap != null){
			int page = Integer.parseInt((String)pagemap.get("page"));
			int size = Integer.parseInt((String)pagemap.get("size"));
			Integer tid = Integer.parseInt((String)pagemap.get("tid"));
			Query query = session.createQuery(hql)
					.setLong(0, tid.longValue())
					.setFirstResult((page-1)*size)
					.setMaxResults(size);
			count = ((Number) query.iterate().next())
		  	         .intValue();
		}
		return count;
	}

	@Override
	public void deleteReplyByBranch(Long[] replys) {
		String hql = "";
		for(int i=0;i<replys.length;i++) {
            if(i==0) {
                hql = "r.rid="+replys[i];
            } else {
                hql =hql + " or r.rid="+replys[i];
            }
        }
		if(!hql.equals("")){
	        Session session= sessionFactory.getCurrentSession();
	        Query q= session.createQuery("update Reply r set r.rstatus = '1' where " + hql);
	        q.executeUpdate();
			logger.info("批量删除留言" + replys);
		}
	}

	@Override
	public void deleteReplyByUser(Long uid) {
		Session session = sessionFactory.getCurrentSession();
		String hql = "update Reply r set r.rstatus = '1' where r.user.id = ? ";
		session.createQuery(hql).setParameter(0, uid)
				.executeUpdate();
	}

	@Override
	public void deleteReplyByTopic(Long tid) {
		Session session = sessionFactory.getCurrentSession();
		String hql = "update Reply r set r.rstatus = '1' where r.topic.tid = ? ";
		session.createQuery(hql).setParameter(0, tid)
				.executeUpdate();
	}

}
